package com.hm.shopping.web.tourists;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hm.base.service.AuthorizationService;
import com.hm.base.vo.LoginSubscriberVo;
import com.hm.common.annotation.WebClientRequestIntercept;
import com.hm.common.util.CommonUtil;
import com.hm.common.util.EncryptUtil.Base64;
import com.hm.shopping.auto.SystemEnvConfigProperties;
import com.hm.shopping.auto.su.BaseController;
import com.hm.shopping.auto.su.R;
import com.hm.shopping.auto.su.helper.HmSessionFactory;
import com.hm.shopping.def.AdvertisingLocationEnum;
import com.hm.shopping.service.AdvertisingService;
import com.hm.shopping.service.ClassificationService;
import com.hm.shopping.service.NewsAnnouncementService;
import com.hm.shopping.service.ShopcartService;
import com.hm.shopping.service.UserService;
import com.hm.shopping.vo.UserVo;
import com.hm.shopping.vo.su.UserAddressVo;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2018年1月17日 下午8:09:17
 * @version 1.0
 * @describe
 */
@Slf4j
@Controller
public class HomeController extends BaseController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private ShopcartService shopcartService;
	
	@Autowired
	private AdvertisingService advertisingService;

	@Autowired
	private ClassificationService classificationService;

	@Autowired
	private NewsAnnouncementService newsAnnouncementService;

	@Autowired
	private SystemEnvConfigProperties systemEnvConfigProperties;

	@Autowired
	private AuthorizationService authorizationService;

	@WebClientRequestIntercept(auth = false)
	@RequestMapping(value = { "/", "", "/index.do" }, method = RequestMethod.GET)
	public String index(Model model) {
		model.addAttribute("topHeadlinesNewsAnnouncement", newsAnnouncementService.getTopHeadlinesNewsAnnouncement());// 获取最新头条新闻公告
		model.addAttribute("topNewsAnnouncement", newsAnnouncementService.getTopNewsAnnouncement());// 获取最新的新闻公告
		model.addAttribute("webClassification", classificationService.getWebClassifications());// 分类列表
		model.addAttribute("copyrightStatement", new String(Base64.decode(systemEnvConfigProperties.getFooterCopyrightStatementPC().toCharArray())));// 版权信息
		model.addAttribute("homePageAdvertisingShuffling", advertisingService.getAdvertisings(AdvertisingLocationEnum.HOME_PAGE_SHUFFLING, 5));// 首页滚动广告
		try {
			model.addAttribute("shopcartGoodsCount", shopcartService.getShopcartGoodsCount(HmSessionFactory.currentUserIdString()));
		}catch(Exception e) {
			model.addAttribute("shopcartGoodsCount", shopcartService.getShopcartGoodsCount(null));
		}
		return "index";
	}

	@WebClientRequestIntercept(auth = false)
	@RequestMapping(value = "login.do", method = RequestMethod.GET)
	public String loginPage(Model model) {
		model.addAttribute("copyrightStatement", new String(Base64.decode(systemEnvConfigProperties.getFooterCopyrightStatementPC().toCharArray())));// 版权信息
		return "login";
	}

	@WebClientRequestIntercept(auth = false)
	@RequestMapping(value = "userLogin.do", method = RequestMethod.POST)
	public String login(Model model, @RequestParam(required = false) String username, @RequestParam(required = false) String password) {

		if (CommonUtil.isAnyEmpty(username, password)) {
			model.addAttribute("copyrightStatement", new String(Base64.decode(systemEnvConfigProperties.getFooterCopyrightStatementPC().toCharArray())));// 版权信息
			model.addAttribute("error", "登陆账号密码不能为空");
			return "login";
		}

		try {
			LoginSubscriberVo subscriberVo = authorizationService.login(username, password);
			request.getSession().setAttribute(R.Session.SUBSCRIBER_SESSION, subscriberVo);
			UserVo userVo = new UserVo();
			{
				userVo.setSubscriberId(HmSessionFactory.currentUserId());
				userVo.setUserAddress(new ArrayList<UserAddressVo>());
			}
			userService.refreshUser(userVo);
			return "redirect:/index.do";
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			model.addAttribute("error", e.getMessage());
		}
		model.addAttribute("copyrightStatement", new String(Base64.decode(systemEnvConfigProperties.getFooterCopyrightStatementPC().toCharArray())));// 版权信息
		return "login";
	}
}
